/*
 * Copyright (c) 2022，公司名称
 * All rights reserved.
 * 文件名称：high_uart.c
 * 摘要：高边串口
 * 当前版本：V1.0.0,WCD,2023/1/10,初版
 * 历史版本：Vx.x.x，编写者/修改者，修改时间，修改内容（重大发布时，总结重点变更内容）
 */
#include "low_includes.h"

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层数据定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
low_uart_rx_struct low_uart_rx_st = {0u};
low_uart_tx_struct low_uart_tx_st = {0u};

static EM_TRUE_FALSE uart30_recv_done = FALSE;

static uint8_t resv_buff[UART_LEN] = {0u};

static uint32_t high_time_tick = 0u;
#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层数据定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
static uint8_t ch;
/*Uart config struct*/
static const UART_Config_t uartCfg =
    {
        19200,
        UART_DATABITS_8,
        UART_PARITY_NONE,
        UART_STOPBITS_1,
        DISABLE,
        64000000 // 40000000
};
static void uart_cbf()
{
  /* receive data */
  ch = UART_ReceiveByte(UART2_ID);
}
void uart_send(uint8_t const *logBuf, uint32_t logLen)
{
  uint32_t i = 0;
  uint8_t ch = 0;
  for (i = 0; i < logLen; i++)
  {
    ch = *(logBuf + i);
    /* Wait untill FIFO is empty */
    while (RESET == UART_GetLineStatus(UART2_ID, UART_LINESTA_TEMT))
      ;

    /* Send data */
    UART_SendByte(UART2_ID, ch);
  }
}
/*
 * 函数介绍: 串口时钟初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void low_uart_clock_init(void)
{
  /*Config UART1 clock, enable UART1 module*/
  CLK_ModuleSrc(CLK_UART2, CLK_SRC_FIRC64M); //   CLK_SRC_OSC40M
  CLK_SetClkDivider(CLK_UART2, CLK_DIV_1);
  SYSCTRL_EnableModule(SYSCTRL_UART2);
}
/*
 * 函数介绍: 串口初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void low_uart_init(void)
{
  /*When UART re-open after close, need to check and clear busy status*/
  while (ERR == UART_WaitBusyClear(UART2_ID, 7000))
  {
    (void)UART_ReceiveByte(UART2_ID);
  }
  /* fifo config */
  const UART_FIFOConfig_t fifoCfg =
      {
          ENABLE, ENABLE, ENABLE, UART_TX_FIFO_HALF, UART_RX_FIFO_CHAR_1};
  UART_FIFOConfig(UART2_ID, &fifoCfg);
  /* Inital UART1 */
  UART_Init(UART2_ID, &uartCfg);
  /* callback function */
  UART_InstallCallBackFunc(UART2_ID, UART_INT_RBFI, uart_cbf);
  /* enable received data available interrupt  启用接收数据可用中断*/
  UART_IntMask(UART2_ID, UART_INT_RBFI, UNMASK);
  /* enable IRQ */
  NVIC_EnableIRQ(UART2_IRQn);
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层函数定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
/*
 * 函数介绍: uart数据校验
 * 参数:buff 数据指针
 * 参数:len 数据长度
 * 返回值: {*}
 * 备注:
 */
static ErrorStatus uart_check_data(uint8_t *buff, uint8_t len)
{
    uint8_t temp = 0x55u;
    uint8_t i = 0u;
    ErrorStatus res = FAIL;
    len -= 1;

    if (buff != NULL)
    {
        uint8_t check_value = buff[len];

        for (i = 0u; i < len; i++)
        {
            temp ^= buff[i];
            temp++;
        }
        if (temp == check_value)
        {
            res = SUCCESS;
        }
        else
        {
            NOP;
        }
    }
    else
    { ;
    }
    return res;
}
/*
 * 函数介绍: 生成串口checksum
 * 参数:buff 数据指针
 * 参数:len 数据长度
 * 返回值: {*}
 * 备注:
 */
static void uart_gen_checksum(uint8_t *buff, uint8_t len)
{
    uint8_t temp = 0x55u;
    uint8_t i = 0u;
    if (buff != NULL)
    {
        len -= 1u;

        for (i = 0u; i < len; i++)
        {
            temp ^= buff[i];
            temp++;
        }
        buff[i] = temp;
    }
    else
    {
        Pointer_Null_Error;
    }
}
/*
 * 函数介绍: 串口30数据收发
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void low_uart30_rx_tx_handle(void)
{
    /*Rx*/
    if (TRUE == uart30_recv_done)
    {
        ErrorStatus check_res = uart_check_data(resv_buff, UART_LEN);
        if (SUCCESS == check_res)
        {
            memcpy(low_uart_rx_st.data, resv_buff, UART_LEN);
        }
        else
        { ;
        }
        uart30_recv_done = FALSE;
    }
    else
    { ;
    }
    /*Tx*/
    if ( 1 == Get1MsTickInterval(high_time_tick,LOW_UART30_TX_TIME) ) /* 10mS */
    {
        high_time_tick = Get1MsTickVal(); /* 获取计数值 */
/*        uart30.writes(&uart30, low_uart_tx_st.data, UART_LEN);*/
#if 0
        low_uart_tx_st.data[0] = '0';
        low_uart_tx_st.data[1] = '1';
        low_uart_tx_st.data[2] = '2';
        low_uart_tx_st.data[3] = '3';
        low_uart_tx_st.data[4] = '4';
        low_uart_tx_st.data[5] = '5';
        low_uart_tx_st.data[6] = '6';
        low_uart_tx_st.data[7] = '7';
#endif
        /*checksum*/
        uart_gen_checksum(low_uart_tx_st.data, UART_LEN);
        uart_send(low_uart_tx_st.data,MDL_8_data);
        //uart30.write_mulit_bytes(&uart30, low_uart_tx_st.data, MDL_8_data);
    }
    else
    { ;
    }
}
#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层函数定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/